﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Medianamik.Core
{
    [Serializable()]
	public class RoleCollection : Collection<string>
	{
		public RoleCollection():base() { }
		public RoleCollection(IEnumerable<string> pValues)
		{
			AddRange(pValues);
		}
		public void AddRange(IEnumerable<string> pValues)
		{
			foreach (var val in pValues)
			{
				Add(val);
			}
		}
		protected override void InsertItem(int index, string item)
		{
			if (item == null || item.Trim().Length == 0)
			{
				throw new CoreException("Value cannot be null or Empty");
			}
			if (item.Contains(','))
			{
				throw new CoreException("Value cannot contain character ','.");
			}
			base.InsertItem(index, item.Trim());
		}
		public override string ToString()
		{
			return string.Join(",", this.Where(r=>!string.IsNullOrEmpty(r)).ToArray());
		}
		
		public static RoleCollection FromString(string pValues)
		{
			var values = (pValues ?? string.Empty).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
			return new RoleCollection(values); 
		}
	}
}
